<template>
  <div class="app-container" style="margin-left: 10px">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo" style="margin-left: 15px">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus" style="margin-left: 15px">
        <el-input
            v-model="queryParams.orderStatus"
            placeholder="订单状态"
            clearable
            @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
            clearable
            v-model="queryParams.dates"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"/>
      </el-form-item>
      <br>
      <el-form-item label="收货人姓名" prop="receiverName" label-width="30%">
        <el-input
            v-model="queryParams.receiverName"
            placeholder="收货人姓名"
            clearable
            @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货人手机" prop="receiverPhone" label-width="30%">
        <el-input
            v-model="queryParams.receiverPhone"
            placeholder="收货人手机"
            clearable
            @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button size="small" icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:orderInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="订单号" align="center" prop="orderNo" width="280"/>
      <el-table-column label="订单总额" align="center" prop="totalAmount"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <span v-if="scope.row.orderStatus === 0">待付款</span>
          <span v-if="scope.row.orderStatus === 1">待发货</span>
          <span v-if="scope.row.orderStatus === 2">已发货</span>
          <span v-if="scope.row.orderStatus === 3">待用户收货，已完成</span>
          <span v-if="scope.row.orderStatus === -1">已取消</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180"/>
      <el-table-column label="收货人姓名" align="center" prop="receiverName"/>
      <el-table-column label="收货人电话" align="center" prop="receiverPhone"/>
      <el-table-column label="详细地址" align="center" prop="receiverAddress"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['order:orderInfo:edit']">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 订单详情对话框 -->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="120px">
        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">订单基本信息</span>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单号">
              {{ form.orderNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单总额">
              {{ form.totalAmount }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="优惠券">
              {{ form.couponAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原价金额">
              {{ form.originalTotalAmount }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运费">
              {{ form.feightFee }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式">
              支付宝
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态">
              {{
                form.orderStatus === 0
                    ? '待支付'
                    : form.orderStatus === 1
                        ? '待发货'
                        : form.orderStatus === 2
                            ? '已发货'
                            : form.orderStatus === -1
                                ? '已取消'
                                : '完成'
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              {{ form.createTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支付时间">
              {{ form.paymentTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单备注">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">收货人信息</span>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货人姓名">
              {{ form.receiverName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货人电话">
              {{ form.receiverPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址标签">
              {{ form.receiverTagName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货详细地址">
              {{ form.receiverAddress }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">订单明细信息</span>
        <el-table :data="form.orderItems" style="width: 100%">
          <el-table-column prop="skuName" label="SKU名称"/>
          <el-table-column label="图片" #default="scope" width="150">
            <img :src="scope.row.thumbImg" width="50"/>
          </el-table-column>
          <el-table-column prop="skuPrice" label="SKU价格" width="120"/>
          <el-table-column prop="skuNum" label="购买数量" width="120"/>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderInfo">
import {listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo} from "@/api/order/orderInfo";
import {parseTime} from "@/utils/ruoyi.js";

const {proxy} = getCurrentInstance();

const orderInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    nickName: null,
    orderNo: null,
    couponId: null,
    totalAmount: null,
    couponAmount: null,
    originalTotalAmount: null,
    feightFee: null,
    orderStatus: null,
    receiverName: null,
    receiverPhone: null,
    receiverTagName: null,
    receiverProvince: null,
    receiverCity: null,
    receiverDistrict: null,
    receiverAddress: null,
    paymentTime: null,
    deliveryTime: null,
    receiveTime: null,
    cancelTime: null,
    cancelReason: null,
  },
  rules: {
    userId: [
      {required: true, message: "会员_id不能为空", trigger: "blur"}
    ],
    orderNo: [
      {required: true, message: "订单号不能为空", trigger: "blur"}
    ],
    totalAmount: [
      {required: true, message: "订单总额不能为空", trigger: "blur"}
    ],
    couponAmount: [
      {required: true, message: "优惠券不能为空", trigger: "blur"}
    ],
    originalTotalAmount: [
      {required: true, message: "原价金额不能为空", trigger: "blur"}
    ],
    feightFee: [
      {required: true, message: "运费不能为空", trigger: "blur"}
    ],
    orderStatus: [
      {
        required: true,
        message: "订单状态【0->待付款；1->待发货；2->已发货；3->待用户收货，已完成；-1->已取消】不能为空",
        trigger: "change"
      }
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
    updateTime: [
      {required: true, message: "更新时间不能为空", trigger: "blur"}
    ],
    delFlag: [
      {required: true, message: "删除标志不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  listOrderInfo(queryParams.value).then(response => {
    orderInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    nickName: null,
    orderNo: null,
    couponId: null,
    totalAmount: null,
    couponAmount: null,
    originalTotalAmount: null,
    feightFee: null,
    orderStatus: null,
    receiverName: null,
    receiverPhone: null,
    receiverTagName: null,
    receiverProvince: null,
    receiverCity: null,
    receiverDistrict: null,
    receiverAddress: null,
    paymentTime: null,
    deliveryTime: null,
    receiveTime: null,
    cancelTime: null,
    cancelReason: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null,
    remark: null
  };
  proxy.resetForm("orderInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  if (queryParams.value.dates && queryParams.value.dates.length === 2) {
    queryParams.value.start = queryParams.value.dates[0];
    queryParams.value.end = queryParams.value.dates[1];
    queryParams.value.dates = null
  }
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOrderInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "详情";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrderInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除订单编号为"' + _ids + '"的数据项？').then(function () {
    return delOrderInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/orderInfo/export', {
    ...queryParams.value
  }, `orderInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
